package com.lagou.controller;

import com.lagou.pojo.Resume;
import com.lagou.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author 应癫
 *
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {
	
	@Autowired
    IResumeService resumeService;
	
	
	
	
	
	@RequestMapping(value="/login", method = RequestMethod.POST)
    public ModelAndView login(String username, String password, HttpSession session) {
        if("admin".equals(username) && "admin".equals(password)) {
        	session.setAttribute("username", username);
        	return new ModelAndView("redirect:list");
        }else {
        	ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("fail");
            return modelAndView;
        }
    }
	
	@RequestMapping(value="/list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",date);
        modelAndView.addObject("list",resumeService.getAll());
        
        modelAndView.setViewName("list");
        return modelAndView;
    }
	
	@RequestMapping(value="/delete", method = RequestMethod.GET)
    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        String id = request.getParameter("id");

        resumeService.delete(id);
        
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",date);
        modelAndView.addObject("list",resumeService.getAll());
        
        modelAndView.setViewName("list");
        return modelAndView;
    }
	
	@RequestMapping(value="/edit", method = RequestMethod.GET)
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		String id = request.getParameter("id");
		ModelAndView modelAndView = new ModelAndView();
		
        if(id != null && !"".equals(id)) {
        	Resume resume = resumeService.get(id);
        	 Date date = new Date();
             modelAndView.addObject("date",date);
             modelAndView.addObject("resume",resume);
        }
       
        
        modelAndView.setViewName("edit");
        return modelAndView;
    }
	
	@RequestMapping(value="/save", method = RequestMethod.POST)
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String id = request.getParameter("id");

		if(id != null && !"".equals(id)) {
			Resume resume = resumeService.get(id);
			resume.setAddress(address);
			resume.setName(name);
			resume.setPhone(phone);
			resumeService.update(resume);
		}else {
			Resume resume = new Resume();
			resume.setAddress(address);
			resume.setName(name);
			resume.setPhone(phone);
			resumeService.save(resume);
		}
        
		return new ModelAndView("redirect:list");
    }
	
}